<template>
<div>
  <el-dialog
  title="选择班级"
  :close-on-click-modal="false"
    :visible.sync="visible">
    <div class="mod-config">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.name" placeholder="班级名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <!-- <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column> -->
        <el-table-column
          type="index"
          header-align="center"
          align="center"
          label="序号"
          width="50">
          
        </el-table-column>
        
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          label="班级名称"
          width="240"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="schoolName"
          header-align="center"
          align="center"
          label="校区">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="selectBtn(scope.row.id, scope.row.price)" size="small">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
  </el-dialog>
  <el-dialog
    title="购买课程"
    :visible.sync="payVisible"
    width="40%"
    center>
    <div style="margin-bottom:10px;">确定要选择该班吗？</div>
    <el-form :model="dataForm" id="pay-dialog">
      <!-- <el-form-item label="支付类型" prop="payType">
        <el-select v-model="dataForm.payType"  placeholder="支付类型">
          <el-option
            v-for="item in payTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
       <el-form-item label="现  金:" prop="cashMoney">
        <el-input v-model="dataForm.cashMoney" type="text" :rows="2"  style="width:200px;"></el-input>
      </el-form-item>
       
       <el-form-item label="微  信:" prop="wxMoney">
        <el-input v-model="dataForm.wxMoney" type="text" :rows="2" style="width:200px;" ></el-input>
      </el-form-item>
      <el-form-item label="支付宝:" prop="zfbMoney">
        <el-input v-model="dataForm.zfbMoney" type="text" :rows="2" style="width:200px;" ></el-input>
      </el-form-item>
      <el-form-item label="备注:" prop="remark">
        <el-input v-model="dataForm.remark" type="textarea" :rows="2"  placeholder="请输入备注内容"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancelPay()">取 消</el-button>
      <el-button type="primary" @click="surePay()">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          name: '',
          payType : '',
          remark: '',
          studentNo: '',
          classId: '',
          cashMoney: '',
          wxMoney: '',
          zfbMoney: '',
          price: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        visible: false,
        payVisible: false,
        payTypeOptions: [
          {
            value: '1',
            label: '微信'
          }, {
            value: '2',
            label: '支付宝'
          },
           {
            value: '3',
            label: '现金'
          }
        ]
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      init(studentNo) {
        this.dataForm.studentNo = studentNo;
        this.visible = true;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/educlass/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'isDeleted':0
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      //选课
      selectBtn(classId,price) {
        this.payVisible = true;
        this.dataForm.classId = classId;
        this.dataForm.price = price;
      },
      //
      cancelPay() {
        this.payVisible = false;
        this.dataForm.payType = ''
        this.dataForm.remark = ''
      },
      surePay() {
        if(!this.dataForm.cashMoney && !this.dataForm.wxMoney && !this.dataForm.zfbMoney){
          return this.$message.error('请输入收款金额');
        }
        this.buyClass();
      },
      //选择购买班级
      buyClass() {
        let orderRecordEntity = [];
        if(this.dataForm.cashMoney) {
          orderRecordEntity.push({
            id: '',
            fee: Number(this.dataForm.cashMoney),
            payType: '3',
            orderNo: '',
          })
        }
        if(this.dataForm.wxMoney) {
          orderRecordEntity.push({
            id: '',
            fee: Number(this.dataForm.wxMoney),
            payType: '1',
            orderNo: '',
          })
        }
        if(this.dataForm.zfbMoney) {
          orderRecordEntity.push({
            id: '',
            fee: Number(this.dataForm.zfbMoney),
            payType: '2',
            orderNo: '',
          })
        }
        this.$http({
          url: this.$http.adornUrl('/sys/payment'),
          method: 'post',
          data: this.$http.adornData({
            'studentNo': this.dataForm.studentNo,
            'classId': [this.dataForm.classId],
            'remark':  this.dataForm.remark,
            'orderRecordEntity': orderRecordEntity,
            'price': Number(this.dataForm.wxMoney || 0) + Number(this.dataForm.zfbMoney || 0) + Number(this.dataForm.cashMoney || 0)
          })
        }).then(({data}) => {
          if(data && data.code == 0) {
            this.$message({
              message: '购买成功',
              type: 'success'
            });
            this.visible = false;
            this.$emit('refreshDataList')
            
          }else{
            this.$message.error(data.msg)
          }
          
          this.payVisible = false;
          this.dataForm = {
            name: '',
            payType : '',
            remark: '',
            studentNo: '',
            classId: ''
          }
        })
      }

    
    }
  }
</script>

<style scoped>
 .teacher-avatar{
   width: 50px;
   height: 50px;
   border-radius: 10px;
 }
 #pay-dialog .el-textarea{
   width: 60%!important;
 }
</style>